package com.health.user.controller;

import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.core.domain.model.LoginUser;
import com.health.common.core.page.TableDataInfo;
import com.health.common.enums.BusinessType;
import com.health.common.utils.SecurityUtils;
import com.health.common.utils.poi.ExcelUtil;
import com.health.user.domain.CommonUser;
import com.health.user.domain.dto.CaptchaDTO;
import com.health.user.domain.vo.UserDetailVO;
import com.health.user.service.ICommonUserService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.health.framework.web.service.TokenService;
import java.util.List;

/**
 * 账号管理Controller
 * 
 * @author zt
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/user/commonUser")
public class CommonUserController extends BaseController
{
    @Autowired
    private ICommonUserService commonUserService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommonUser commonUser)
    {
        startPage();
        List<CommonUser> list = commonUserService.selectCommonUserList(commonUser);
        return getDataTable(list);
    }

    /**
     * 导出账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:export')")
    @Log(title = "账号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonUser commonUser)
    {
        List<CommonUser> list = commonUserService.selectCommonUserList(commonUser);
        ExcelUtil<CommonUser> util = new ExcelUtil<CommonUser>(CommonUser.class);
        util.exportExcel(response, list, "账号管理数据");
    }

    /**
     * 获取账号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(commonUserService.selectCommonUserByUserId(userId));
    }

    /**
     * 新增账号管理
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:add')")
    @Log(title = "账号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonUser commonUser) throws RuntimeException {
        return toAjax(commonUserService.insertCommonUser(commonUser));
    }

    /**
     * 修改账号管理
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:edit')")
    @Log(title = "账号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonUser commonUser)
    {
        return toAjax(commonUserService.updateCommonUser(commonUser));
    }

    /**
     * 删除账号管理
     */
    @PreAuthorize("@ss.hasPermi('user:commonUser:remove')")
    @Log(title = "账号管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(commonUserService.deleteCommonUserByUserIds(userIds));
    }

    @GetMapping("/detail/{userId}")
    public AjaxResult detail(@PathVariable("userId") Long userId) {
        return success(commonUserService.selectUserDetailVOByUserId(userId));
    }
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Long userId = SecurityUtils.getUserId();
        String imgUrl = commonUserService.uploadImg(file, userId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 更新缓存用户头像
        loginUser.getUser().setAvatar(imgUrl);
        tokenService.setLoginUser(loginUser);
        return success(imgUrl);
    }
    @PutMapping("/updatePhone")
    public AjaxResult updatePhone(@RequestBody CaptchaDTO captchaDTO){
        commonUserService.updatePhone(captchaDTO, SecurityUtils.getUserId());
        return success();
    }
    @PutMapping("/updateEmail")
    public AjaxResult updateEmail(@RequestBody CaptchaDTO captchaDTO){
        commonUserService.updateEmail(captchaDTO, SecurityUtils.getUserId());
        return success();
    }
    @PutMapping("/detail/update")
    public AjaxResult updateDetail(@RequestBody UserDetailVO userDetail) {
        userDetail.setUserId(SecurityUtils.getUserId());
        commonUserService.updateDetail(userDetail);
        return success();
    }
}
